const path = require("path");
const webpack = require("webpack");
const utils = require("./buildutils");
const CircularDependencyPlugin = require("circular-dependency-plugin");
const lzString = require("lz-string");

module.exports = {
  mode: "development",
  devtool: "cheap-source-map",
  entry: {
    "bundle.js": [path.resolve(__dirname, "../src/js/main.js")],
  },
  watch,
  node: {
    fs: "empty",
  },
  resolve: {
    alias: {
      "global-compression": path.resolve(
        __dirname,
        "..",
        "src",
        "js",
        "core",
        "utils",
        "lzstring.js"
      ),
    },
    mainFiles: ["index"],
  },
  context: path.resolve(__dirname, ".."),
  plugins: [
    new webpack.DefinePlugin({
      // 断言，但是不知道有啥用
      assert: "window.assert",
      assertAlways: "window.assert",
      abstract:
        'window.assert(false, "abstract method called of: " + (this.name || (this.constructor && this.constructor.name)))',
      // 是否有断言
      G_HAVE_ASSERT: "true",
      // 什么环境
      G_APP_ENVIRONMENT: JSON.stringify("dev"),
      G_TRACKING_ENDPOINT: JSON.stringify(
        lzString.compressToEncodedURIComponent("http://localhost:10005/v1")
      ),
      G_IS_DEV: "true",
      G_IS_PROD: "false",
      G_IS_RELEASE: "false",
      G_IS_MOBILE_APP: "false",
      G_IS_BROWSER: "true",
      G_IS_STANDALONE: standalone ? "true" : "false",
      G_BUILD_TIME: "" + new Date().getTime(),
      G_BUILD_COMMIT_HASH: JSON.stringify(utils.getRevision()),
      G_BUILD_VERSION: JSON.stringify(utils.getVersion()),
      G_ALL_UI_IMAGES: JSON.stringify(utils.getAllResourceImages()),
    }),

    // 防止相互导入文件
    new CircularDependencyPlugin({
      exclude: /node_modules/,
      failOnError: true,
      allowAsyncCycles: false,
      cwd: path.join(__dirname, "..", "src", "js"),
    }),
  ],
  module: {
    rules: [
      // json文件被压缩
      {
        test: /\.json$/,
        enforce: "pre",
        use: ["./gulp/loader.compressjson"],
        type: "javascript/auto",
      },
      // 不处理
      { test: /\.(png|jpe?g|svg|gif)$/, loader: "ignore-loader" },
      // 可以导入md文件
      {
        test: /\.md$/,
        use: [
          {
            loader: "html-loader",
          },
          "markdown-loader",
        ],
      },
      // js文件
      {
        test: /\.js$/,
        enforce: "pre",
        exclude: /node_modules/,
        use: [
          {
            // 删除注释代码
            loader: "webpack-strip-block",
            options: {
              // 自定义注释的开始结尾
              start: "typehints:start",
              end: "typehints:end",
            },
          },
        ],
      },
      // 将这个worker.js改成worker文件，可以使用多线程，线程间可以互相通信
      {
        test: /\.worker\.js$/,
        use: {
          loader: "worker-loader",
          options: {
            inline: "fallback",
          },
        },
      },
    ],
  },
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "..", "build"),
  },
};
